from Spider.Base import Base
from Utils.ProxyRequest import ProxyRequest

class Downloader(Base):
    def __init__(self,logger,proxy_radio):
        super(Downloader, self).__init__(logger, proxy_radio)

    def exec(self, imageUrl):
        """
        :param imageUrl:
        :return:
        """
        state = False
        content = None  # 成功则bytes类型

        headers = {
            "User-Agent":self.getRandomUa()
        }

        res = ProxyRequest(proxy_radio=self.proxy_radio).get(url=imageUrl, headers=headers, timeout=20)
        if 200 == res.status_code:
            # print(type(res.text)) # str
            # print(type(res.content)) # bytes
            content = res.content
            state = True
        else:
            pass
            # self.logger.error("status_code=%d|%s" % (res.status_code, imageUrl))

        return state, content